package com.linkcard.list.persistence;

import java.sql.SQLException;
import java.util.List;

import org.apache.commons.beanutils.BeanUtils;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.linkcard.commen.tools.Page;
import com.linkcard.list.model.News;

public class NewsManagerDaoImpl extends HibernateDaoSupport implements
		NewsManagerDao {

	//添加新闻
	public boolean addNews(News news) {
	    try
	    {
	    	this.getHibernateTemplate().save(news);
	    }catch(Exception e)
	    {
	    	e.printStackTrace();
	    	return false;
	    }
		return true;
	}

	//获得新闻列表
	public List newsList(final Page pageObj,final String searchStr) {
		List list=(List)this.getHibernateTemplate().execute(new HibernateCallback(){

			public Object doInHibernate(Session session)throws HibernateException, SQLException {
				String hql="from News news,User user,Category category where news.publisher=user.id and news.categoryId=category.id ";
				if(!searchStr.equals(""))
				{
					String[] str=searchStr.split("_");
					if(!str[0].equals("-2"))
					{
					  hql+=" and news.categoryId="+str[0];
					}
					if(!str[1].equals("-2"))
					{
					  hql+=" and news.subCategoryId="+str[1];
					}
					if(str.length==3)
					{
					hql+=" and (news.title like '%"+str[2]+"%'";
					hql+=" or news.content like '%"+str[2]+"%')";
					}
				}
				
				   hql+=" order by news.recommend desc ,news.datetime desc ";
				
				int rowCount = getRowCount(hql);
				  Query query=session.createQuery(hql);
				  query.setFirstResult((pageObj.getCurrentPage()-1)*pageObj.getPageSize());
				  query.setMaxResults(pageObj.getPageSize());
		         pageObj.setRowCount(rowCount);
		         pageObj.setTotalPage(rowCount);
		         pageObj.setPreNextPage();
				return query.list();
			}
			
		});
		return list;
	}
	/**
	 * 获得记录数
	 */
	public int getRowCount(final String hql) {
		Integer rowCount = 0;
		rowCount = (Integer) this.getHibernateTemplate().execute(
				new HibernateCallback() {
					public Object doInHibernate(Session session)
							throws HibernateException, SQLException {
						return session.createQuery(hql).list().size();
					}
				});
		return rowCount;
	}

	//删除新闻
	public boolean deleteNews(final String ids) {
	 boolean isSuccess=(Boolean)	this.getHibernateTemplate().execute(new HibernateCallback(){
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				try
				{
				String hql="delete from News news where news.id in("+ids+")";
				Query query=session.createQuery(hql);
				query.executeUpdate();
				}catch(Exception e)
				{
					e.printStackTrace();
					return false;
				}
				return true;
			}
		});
		return isSuccess;
	}

	//查找新闻
	public News findNews(Integer id) {
		News news=(News)this.getHibernateTemplate().get(News.class,id);
		return news;
	}

	//修改新闻
	public boolean modifyNews(News news){
		try
		{
		News n=(News)this.getHibernateTemplate().get(News.class, news.getId());
		news.setDatetime(n.getDatetime());
		news.setPublisher(n.getPublisher());
		BeanUtils.copyProperties(n, news);
		this.getHibernateTemplate().update(n);
		}catch(Exception e)
		{
			e.printStackTrace();
			return false;
		}
		return true;
	}
}
